package com.example.demo.controller;

import com.example.demo.model.Blog;
import com.example.demo.model.User;
import com.example.demo.service.BlogService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;

@RestController
public class UpdateController {
    @Resource
    private BlogService blogService;
    @RequestMapping("/updateLoad")
    public Blog updateLoad(String blogId) {
        Blog blog = blogService.getBlogByBid(Integer.parseInt(blogId));
        return blog;
    }
    @RequestMapping("/update")
    public Object updateBlog(String blogId, @RequestBody Blog blog, @SessionAttribute(value = "user",required = false)User user) {
        HashMap<String, Object> map = new HashMap<>();
        blog.setContent(blog.getContent().trim());
        blog.setTitle(blog.getTitle().trim());
        if (blog.getContent() == null || "".equals(blog.getContent()) || blog.getTitle() == null || "".equals(blog.getTitle())) {
            map.put("message", "正文或者标题不能为空!");
            return map;
        }
        blog.setBlogId(Integer.parseInt(blogId));
        blog.setUserId(user.getUserId());
        blogService.updateBlog(blog);
        return map;
    }
}
